! ivf的<n>语法可以在format语句中把n转为格式的重复次数
! 使用标准的Fortran语法实现这个功能
module string_trans_mod
    implicit none
contains
    function form(format,nums)result(res)
        character(len=*),intent(in)::format
        integer,intent(in)::nums(:)
        character(len=:),allocatable::res
        integer::idx,i
        res=format
        i=0
        do
            idx=index(res,"<>")
            if(idx==0)exit
            i=i+1
            if(i>size(nums))error stop "The size of array 'nums' less than the number of '<>'"
            res=res(:idx-1)//tostring(nums(i))//res(idx+2:)        
        end do
    end function form
    
    function tostring(num)result(res)
        integer,intent(in)::num
        character(len=10)::buffer
        character(len=:),allocatable::res
        write(buffer,"(i10)")num
        res=trim(adjustl(buffer))
    end function tostring
end module string_trans_mod

program main
    use string_trans_mod
    implicit none
    
    test_format:block
        !!测试格式化
        integer,parameter::n=4
        integer,parameter::m=3
        real::a(n,n),b(m,m)
        integer::i
        call random_number(a) 
        call random_number(b)
        write(*,form("(<>f8.4)",[n]))a
        write(*,form("(<>f8.4)",[m]))b
        do i=1,10
            write(*,form("(<>x,A<>)",[11-i,2*i-1]))repeat("*",20)
        end do
    end block test_format

    test_filename:block
        !! 生成文件名
        character(:),allocatable::filename
        integer::i
        do i=1,10
            filename=form("data<>.bin",[i])
            write(*,*)filename
        end do
    end block test_filename
end program main
输出
  0.0167  0.5545  0.7795  0.6301
  0.3485  0.8260  0.5498  0.2492
  0.0522  0.9396  0.9741  0.5452
  0.9335  0.3077  0.6807  0.5890
  0.3529  0.3885  0.0408
  0.2170  0.2719  0.8879
  0.6171  0.7485  0.4313
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
 data1.bin
 data2.bin
 data3.bin
 data4.bin
 data5.bin
 data6.bin
 data7.bin
 data8.bin
 data9.bin
 data10.bin